package com.sk.rpcAssistNetty.provideAssist;

import com.sk.rpcAssistNetty.dataManager.DefaultProviderDataManager;
import com.sk.rpcAssistNetty.dataManager.ProviderDataManager;
import com.sk.rpcAssistNetty.io.protocol.ProtocolManage;
import com.sk.rpcAssistNetty.io.protocol.SerializeProtocol;
import com.sk.rpcAssistNetty.policy.ServiceBeanDefinitionRegistry;

import java.util.Map;

/**
 * description：
 *
 * @author 孙文斌
 * @Date: 2021/2/18 16:01
 */
public class ProviderAssistStrategyContent {

    //策略接口
    private static ProviderAssistStrategyInter current;

    public static void doStart() {

        if (current == null) {
            ProviderAssistStrategyInter current = null;
            //判断当前ProvideAssistStrategyInter有无实现类
            Map<String, ProviderAssistStrategyInter> beanMap = ServiceBeanDefinitionRegistry.applicationContext.getBeansOfType(ProviderAssistStrategyInter.class);
            //用户提供了策略
            if (!beanMap.isEmpty()) {
                if (beanMap.size() > 1) {
                    throw new RuntimeException("Expected only one implementation class for ProvideAssistStrategyInter interface, but found multiple,interfaces info :" + ProviderAssistStrategyInter.class.getName());
                } else {
                    current = (ProviderAssistStrategyInter) beanMap.values().toArray()[0];
                }
            } else {
                //使用默认的策略
                ProviderDataManager providerDataManager = null;
                SerializeProtocol serializeProtocol = null;
                //判断用户有无提供ProviderDataManager和MessageProtocolInter
                Map<String, ProviderDataManager> dataManagerMap = ServiceBeanDefinitionRegistry.applicationContext.getBeansOfType(ProviderDataManager.class);
                Map<String, SerializeProtocol> messageProtocolMap = ServiceBeanDefinitionRegistry.applicationContext.getBeansOfType(SerializeProtocol.class);
                if(dataManagerMap.isEmpty()){
                    providerDataManager = new DefaultProviderDataManager();
                }else if (!dataManagerMap.isEmpty() && dataManagerMap.size() > 1) {
                    throw new RuntimeException("Expected only one implementation class for ProviderDataManager interface, but found multiple,interfaces info :" + ProviderDataManager.class.getName());
                }else {
                    providerDataManager = (ProviderDataManager) dataManagerMap.values().toArray()[0];
                }
                if(messageProtocolMap.isEmpty()){
                    serializeProtocol = ProtocolManage.getInstance().getDefaultProtocol();
                }else if (!messageProtocolMap.isEmpty() && messageProtocolMap.size() > 1) {
                    throw new RuntimeException("Expected only one implementation class for SerializeProtocol interface, but found multiple,interfaces info :" + SerializeProtocol.class.getName());
                } else {
                    serializeProtocol = (SerializeProtocol) messageProtocolMap.values().toArray()[0];
                }
                current = new DefaultProviderAstStrategyNetty(providerDataManager,serializeProtocol);
            }
        }
    }
}
